'''
Created on Feb 25, 2011

@author: Chris
'''
from Tkinter import *
import tkMessageBox
import tkFileDialog
import MainFrame
#import ProjectTester


class FrameController(object):
    '''
    classdocs
    '''


    def __init__(self, anonymouth,projectTester=None):
        '''
        Constructor
        '''
        self.anon = anonymouth
        
        if projectTester!=None:
            projectTester.setController(self)
        self.frame = MainFrame.MainFrame(self, projectTester)
        self.frame.start()
        
    def trainCorpus(self):
        self.anon.trainCorpus()    
    
    #Pass it back up
    def submit(self,revision,timestamp, isCompare):
        featureSuggestions = self.anon.submit(revision,timestamp)
        
        if not isCompare:
            self.frame.addSuggestions(featureSuggestions)
    
    def addRevision(self,document,timeStamp):
        self.anon.addRevision(document,timeStamp)
    
    def revertToRevision(self,revisionIndex):
        return self.anon.getRevisionFromIndex(revisionIndex)
        
        
    def updateDocText(self, importedFileName):
        '''Call MainFrame to update the text Area of it with the imported document text from the Project Wizard'''
        importFile = open(importedFileName, 'r')
        importFileString = importFile.read()
        self.setTextArea(importFileString)
        importFile.close()
    
    def setTextArea(self,text):
        self.frame.setTextArea(text)
        
    def useSuggestion(self, suggestionNumber):
        '''Replace the text in the text area with the replacement text of the Suggestion in the suggestion list box in MainFrame'''
    
    def newProject(self):
        self.anon.newProject(self.frame,self.frame.master,self)
    
    def saveProject(self,frame,promptForFilename):
        self.anon.saveProject(frame,promptForFilename)
        
    def openProject(self,frame,filename=None):
        self.anon.openProject(self,filename)

    def setFileHandler(self,fileHandler):
        self.anon.setFileHandler(fileHandler)
    
    def removeCorpusFolder(self,foldername):
        self.anon.removeCorpusFolder(foldername)
        
    def addCorpusFolder(self,foldername):
        return self.anon.addCorpusFolder(foldername)
    
    def populateListWithCorpusFolders(self,lstCorpus):
        #list all folders from the corpus
        corpusFolders = self.anon.getCorpusPaths()
        lstCorpus.delete(0,END)
        for folder in corpusFolders:
            lstCorpus.insert(END,folder)
			
    def setCurrentMatch(self):
        return self.anon.currentMatch
    
    def setCurrentAuthor(self):
        return self.anon.currentAuthor
    
    def showWarning(self,title,text):
        tkMessageBox.showwarning(title,text)
        
    def promptOpenFile(self,initialDirectory=None):
        return tkFileDialog.askopenfilename(initialdir=initialDirectory)
        
    def promptOpenFolder(self,initialDirectory=None):
        return tkFileDialog.askdirectory(initialdir=initialDirectory)
        
        